/**
 * @(#)PDDataAccessCommon.java 1.0 2013/9/4
 * Copyright 2013 PD, Inc. All rights reserved.
 */
package cn.broadin.app.hotel.stb.impl.common;
import java.util.HashMap;

import cn.broadin.app.hotel.stb.IPDDataAccess;
import android.content.Context;
import android.media.AudioManager;
import android.util.Log;
import android.webkit.JavascriptInterface;

/**
 * PDDataAccessCommon 数据访问通用类
 * @author fenghui
 *
 */
public class PDDataAccessCommon implements IPDDataAccess{

	//日志tag
	private static final String TAG = "PDDataAccessCommon";
	//context 对象
	private Context context = null;
	//用于保存数据
	private HashMap<String, String> setMap = new HashMap<String, String>();
	//当前音量实际值
	public static int currVolumnValue = 0;
	//当前音量显示值
	private int currVolumnShow = 0;
	//是否第一次获取
	private boolean isFirstGet = true;
	//AudioManager
	AudioManager audioMa = null;
	
	public PDDataAccessCommon(Context ctx) {
		this.context = ctx;
		audioMa = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
	}
	
	@Override
	@JavascriptInterface
	public void setInfo(String className, String infoName, String infoValue) {
		if (className.equals("MediaSetting") && infoName.equals("OutputVolumn")) {
			//if(isMuteState) {return;}
			currVolumnShow = Integer.parseInt(infoValue);
			if (currVolumnShow % 2 == 0 && currVolumnShow < 30) {
				currVolumnValue = currVolumnShow / 2;
				audioMa.setStreamVolume(AudioManager.STREAM_MUSIC, currVolumnValue, AudioManager.ADJUST_SAME);
			} else if (currVolumnShow == 1) {
				currVolumnValue = 1;
				audioMa.setStreamVolume(AudioManager.STREAM_MUSIC, currVolumnValue, AudioManager.ADJUST_SAME);
			}
		}
		Log.i(TAG, "setInfo key is :" + className+infoName + ", vlaue is " +  infoValue + ".");
		setMap.put(className + infoName, infoValue);
	}

	@Override
	@JavascriptInterface
	public String getInfo(String className, String infoName) {
		if (className.equals("MediaSetting") && infoName.equals("OutputVolumn")) {
			int audioTemp = audioMa.getStreamVolume(AudioManager.STREAM_MUSIC);
			if (isFirstGet) {
				isFirstGet = false;
				currVolumnValue = audioTemp;
				currVolumnShow = audioTemp * 2;
			}
			Log.i(TAG, "getInfo key is :" + className+infoName + ", vlaue is " +  currVolumnShow + ".");
			return currVolumnShow + "";
		}
		String infoVal = setMap.get(className + infoName);
		Log.i(TAG, "getInfo key is :" + className+infoName + ", vlaue is " +  infoVal + ".");
		return infoVal;
	}
	
	/**
	 * 当前音量
	 * @return
	 */
	public static int getCurrentVol() {
		return currVolumnValue;
	}
}